package com.neusoft.resturant.controller;

import com.alibaba.fastjson2.JSON;
import com.neusoft.resturant.common.utils.R;
import com.neusoft.resturant.entity.Order;
import com.neusoft.resturant.entity.OrderDetail;
import com.neusoft.resturant.entity.ResDishes;
import com.neusoft.resturant.entity.SysUser;
import com.neusoft.resturant.mapper.ResDishesMapper;
import com.neusoft.resturant.service.IOrderDetailService;
import com.neusoft.resturant.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("orderdels")
public class OrderDetailController extends BaseController {
    @Autowired
    private IOrderDetailService orderDetailService;
    @Autowired
    private ResDishesMapper resDishesMapper;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping("get")
    public R selectAll(Integer oid) {
        return R.success(orderDetailService.selectAll(oid));
    }

    @RequestMapping("add")
    public R addOrderDetail(Integer dishId, Integer oid, Integer num, HttpSession session) {
        String token = (String) session.getAttribute("token");
        if (token == null) {
            return R.error("请先登录");
        }
        String userToString = (String) redisTemplate.opsForValue().get(token);
        SysUser sysUser = JSON.parseObject(userToString, SysUser.class);
        ResDishes resDishes = resDishesMapper.selectResDishesById(Long.valueOf(dishId));
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setDishId(Integer.valueOf(resDishes.getId().toString()));
        orderDetail.setDishName(resDishes.getName());
//        orderDetail.setDishPrice(resDishes.getPrice().doubleValue());
        orderDetail.setOrderId(oid);
        orderDetail.setQuantity(num);
        Double sum = resDishes.getPrice().doubleValue() * num;
        orderDetail.setDishPrice(sum);
        Integer i = orderDetailService.insertOrderDetail(orderDetail);
        return new R().success(i);
    }
}
